/**
 * 神州信息(2012)
 */
package net.abbp.report.view.cell.editor.eform;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridBagLayout;

import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.reportgear.core.swing.pane.base.EditorPane;
import com.reportgear.core.swing.pane.base.JBaseDialog;

import net.abbp.engine.ui.model.component.Label;
import net.abbp.report.model.Cell;
import net.abbp.report.view.cell.editor.AbstractCellEditor;
import net.abbp.report.view.grid.Grid;
import net.abbp.swing.layout.LayoutUtils;

/**
 * 标签编辑器
 * 
 * @version 1.0
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ABBP 1.0
 * 
 *        <pre>
 * 历史：
 *      建立: Aug 1, 2012 lexloo
 * </pre>
 */
public class LabelEditor extends AbstractCellEditor {
    private Label label;

    @Override
    public Component getCellEditorComponent(Grid grid, Cell cell) {
        LabelEditPane editorPane = new LabelEditPane();

        editorPane.populate((Label) cell.getValue());

        if (editorPane.showDialog(grid) == JBaseDialog.RESULT_OK) {
            this.label = editorPane.update();
        }

        return null;
    }

    @Override
    public Object getCellEditorValue() {
        return this.label;
    }

    private static class LabelEditPane extends EditorPane<Label> {
        private static final long serialVersionUID = -5535278517462058648L;
        private JTextField txtValue;
        private Label value;

        public LabelEditPane() {
            this.setLayout(new GridBagLayout());

            this.add(new JLabel("标签："), LayoutUtils.newGridBagConstraints(0, 0));
            txtValue = new JTextField();
            txtValue.setPreferredSize(new Dimension(120, 24));
            this.add(txtValue, LayoutUtils.newGridBagConstraints(1, 0));
        }

        @Override
        public void populate(Label value) {
            this.value = value;
            this.txtValue.setText(value.getCaption());
        }

        @Override
        public Label update() {
            this.value.setCaption(this.txtValue.getText().trim());

            return this.value;
        }

        @Override
        public int showDialog(Component owner) {
            return this.showDialog(SwingUtilities.getWindowAncestor(owner), "标签", 300, 120);
        }
    }

}
